package com.ltmonitor.web.controller;

import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ltmonitor.entity.BasicData;
import com.ltmonitor.entity.DriverInfo;
import com.ltmonitor.entity.PlatformState;
import com.ltmonitor.entity.Role;
import com.ltmonitor.entity.SystemConfig;
import com.ltmonitor.entity.UserInfo;
import com.ltmonitor.service.IPlatformStateService;
import com.ltmonitor.service.ISystemConfigService;
import com.ltmonitor.web.util.JsonMessage;

/**
 * 平台参数配置
 * 
 * @author DELL
 * 
 */
@Controller
@RequestMapping("/systemConfig")
public class SystemConfigAction extends GenericAction{

	@Autowired
	protected IPlatformStateService platformStateService;

	@Autowired
	protected ISystemConfigService systemConfigService;
	
	

	protected String EditViewName = "/systemConfig/editSystemConfig";
	
	@RequestMapping("/view.action")
	public String view(
			Model m) {
		try {
			SystemConfig sc = this.systemConfigService.getSystemConfig();
			m.addAttribute("entity", sc);

		} catch (Exception ex) {
			log.error(ex.getMessage(), ex);
			m.addAttribute("message", "发生错误:" + ex.getMessage());
		}
		return "/systemConfig/editSystemConfig";
	}
	
	/**
	 * 新增后的保存操作
	 * 
	 * @return
	 */
	@RequestMapping("/save")
	public String save(SystemConfig d, Model m) throws Exception {

		try {
			//d.setMainDriver(mainDriver);
			//一个车只能有一个主驾驶，需要将同一车的其他主驾驶设置为false
			this.systemConfigService.saveOrUpdate(d);

			m.addAttribute("message", "保存成功");
		} catch (Exception ex) {
			log.error(ex.getMessage(), ex);
			m.addAttribute("message", ex.getMessage());
		}
		m.addAttribute("entity", d);

		return "/systemConfig/editSystemConfig";
	}
	
	
	/**
	 * 获取平台状态
	 * @return
	 */
	@RequestMapping("/platformState.action")
	@ResponseBody
	public JsonMessage platformState()
	{
		try {
			PlatformState ps = this.platformStateService.getPlatformState();
			
			return json(true, ps);
		} catch (Exception e) {
			log.error(e.getMessage(), e);
			
			return json(false, e.getMessage());
		}
	}

}
